package fcl.mylife;

import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

import fcl.platform.R;

public class LifeMapActivity extends MapActivity {
	
	private Projection projection; 
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        MapController mapControl;
        
        setContentView(R.layout.mapview);
        
        //load google map
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setSatellite(false);
        mapView.setBuiltInZoomControls(true);
        mapControl = mapView.getController();
        mapControl.setZoom(12);
        
        
        //draw overlay item
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.service_on);
        MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable);  
        
//        Drawable drawable1 = this.getResources().getDrawable(R.drawable.greencircle);
//        MyItemizedOverlay itemizedoverlay1 = new MyItemizedOverlay(drawable1); 
        
        
        projection = mapView.getProjection();
        mapOverlays.add(new MyOverlay());
        
        //define a geopoint
        
        MapController mc = mapView.getController();
      
        String coordinates[] = {"1.289478890541", "103.80733282853"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
 
        GeoPoint point = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));
       
        mc.animateTo(point);
 
        OverlayItem overlayitem = new OverlayItem(point, "hello", "I'm in Singapore!");
        
        //green circle
//        GeoPoint point2 = new GeoPoint(35410000, 139460000);
//        OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
        
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);

        //green circle
//        itemizedoverlay1.addOverlay(overlayitem2);
//        mapOverlays.add(itemizedoverlay1);
        
    }
    class MyOverlay extends Overlay{

        public MyOverlay(){

        }   

        public void draw(Canvas canvas, MapView mapv, boolean shadow){
            super.draw(canvas, mapv, shadow);

            Paint mPaint = new Paint();
            mPaint.setDither(true);
            mPaint.setColor(Color.BLUE);
            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(3);

            String coordinates[] = {"1.289478890541", "103.80733282853"};
            double lat = Double.parseDouble(coordinates[0]);
            double lng = Double.parseDouble(coordinates[1]);
     
            GeoPoint gP1 = new GeoPoint(
                (int) (lat * 1E6), 
                (int) (lng * 1E6));
            
            String coordinates2[] = {"1.31776", "103.69006"};
            double lat2 = Double.parseDouble(coordinates2[0]);
            double lng2 = Double.parseDouble(coordinates2[1]);
     
            GeoPoint gP2 = new GeoPoint(
                (int) (lat2 * 1E6), 
                (int) (lng2 * 1E6));
            
            //GeoPoint gP1 = new GeoPoint(19240000,-99120000);
            //GeoPoint gP2 = new GeoPoint(37423157, -122085008);

            Point p1 = new Point();
            Point p2 = new Point();

            Path path = new Path();

            Projection projection = mapv.getProjection();
            projection.toPixels(gP1, p1);
            projection.toPixels(gP2, p2);

            path.moveTo(p2.x, p2.y);
            path.lineTo(p1.x,p1.y);

            canvas.drawPath(path, mPaint);
        }
    }
        
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}